home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 4565 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  1.9 KB

  1. Path: sundog.tiac.net!smayo
  2. From: smayo@tiac.net (Scott Mayo)
  3. Newsgroups: comp.lang.c++
  4. Subject: So I have this pointer-to-member function...
  5. Date: Tue, 30 Jan 1996 19:50:55 -0500
  6. Organization: The Internet Access Company
  7. Message-ID: <d4c1.smail.smayo@tiac.net>
  8. NNTP-Posting-Host: sunspot.tiac.net
  9.  
  10. I'm trying to do something in C++ which instinct tells me shouldn't be hard to
  11. arrange, but the compiler isn't cooperating.
  12.  
  13. I have a class, call it C, whose purpose in life is to acquire an input
  14. stream, parse it into tokens, and then have the tokens tell it which member
  15. functions to call. For token "A" call member function A, and so on. An array
  16. of struct {char *token; void (C::*f)();} fits the bill well enough to manage
  17. that.
  18.  
  19. Now the tricky part. I want to derive class C1 and C2 from C. And I want C1
  20. (and C2, etc) to have their own such table for mapping tokens to member
  21. functions; in the absence of a match, I want to go up and search C's table and
  22. use what I find there.
  23.  
  24. "Ah," I innocently thought, "I'll just make each array of struct a static
  25. variable inside the class definition it goes with, and then it will be easy to
  26. automatically search the right table at the right time."
  27.  
  28. The compiler will have none of it. It takes one glance at the static struct
  29. {char*name; void (C::*f)();} list[] = {"A", C::A}; and has a hissy fit at the
  30. attempt to assign initializers. It's fine outside the class definition; it
  31. won't work inside.
  32.  
  33. I can clearly just put the arrays outside the class definitions, and just
  34. teach the class "search" function to know which array to go after, but it
  35. seems crass. That array is logically a part of the class behaviour, and if I
  36. end up creating a lot of these subclasses, I don't want to rely on
  37. remembering to tell each search function which magic array to use.
  38.  
  39. Suggestions? Am I missing something simple and useful here? Thanks. Mail
  40. appreciated, posting might be missed.
  41.  
  42.